package com.shzu.jwc.ty.service;

import java.util.List;

import com.shzu.jwc.ty.bean.TheoreticalExaminationAppointment;
import com.shzu.jwc.ty.bean.TheoreticalExaminationAppointmentExample;
import com.shzu.jwc.ty.dao.TheoreticalExaminationAppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shzu.jwc.ty.bean.ViewTheoreticalExaminationAppointment;
import com.shzu.jwc.ty.bean.ViewTheoreticalExaminationAppointmentExample;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.dao.ViewTheoreticalExaminationAppointmentMapper;

@Service
public class JwcKnowledgeScoreservice {


	@Autowired
	TheoreticalExaminationAppointmentMapper mapper;
	
	@Autowired
	ViewTheoreticalExaminationAppointmentMapper vmapper;
	
	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	SystemYearservice systemYearservice;

	@Autowired
	SystemConfigservice systemConfigservice;
	
	public Integer countknowledgescore() {
		return (int)mapper.countTheoreticalExaminationscore(systemYearservice.getoneyearby(Semester.getnow()));
	}
	
	public Integer countselectknowledgescore(Integer theoreticalExaminationId) {
		if(theoreticalExaminationId==0)
			theoreticalExaminationId=null;
		return (int)mapper.countselectTheoreticalExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),theoreticalExaminationId);
	}
	
	public List<TheoreticalExaminationAppointment> getknowledgescore(Page page){

		return mapper.getTheoreticalExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),page.getStartPos(),page.getPageSize());
	}
	
	public List<TheoreticalExaminationAppointment> selectknowledgescore(Integer theoreticalExaminationId,Page page){
		if(theoreticalExaminationId==0)
			theoreticalExaminationId=null;
		return mapper.selectTheoreticalExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),theoreticalExaminationId,page.getStartPos(),page.getPageSize());
	}
	
	public List<TheoreticalExaminationAppointment> upselectknowledgescore(Integer userStudentInfoYear,String userStudentInfoCollege,Integer theoreticalExaminationId){
		if(userStudentInfoYear==0)
			userStudentInfoYear=null;
		if(userStudentInfoCollege.equals("0"))
			userStudentInfoCollege=null;
		if(theoreticalExaminationId==0)
			theoreticalExaminationId=null;
		return mapper.upselectTheoreticalExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),userStudentInfoYear,userStudentInfoCollege,theoreticalExaminationId);
	}
	
	public List<TheoreticalExaminationAppointment> searchknowledgescore(String userStudentInfoNum){
		TheoreticalExaminationAppointmentExample example=new TheoreticalExaminationAppointmentExample();
		TheoreticalExaminationAppointmentExample.Criteria criteria=example.createCriteria();
		criteria.andSystemYearIdEqualTo(systemYearservice.getoneyearby(Semester.getnow()));
		criteria.andUserStudentInfoNumEqualTo(userStudentInfoNum);
		criteria.andTheoreticalExaminationAppointmentStateEqualTo(1);
		return mapper.selectByExample(example);
	}

	public Integer updatescore(double theoreticalExaminationAppointmentScore,Integer theoreticalExaminationAppointmentId){
		return mapper.updatescore(theoreticalExaminationAppointmentScore,theoreticalExaminationAppointmentId);
	}

	public Double updateMultscore(String name,Double score,Integer theoreticalExaminationAppointmentId){
		if(name.equals("theoreticalExaminationAppointmentScore"))
			mapper.updatescore(score,theoreticalExaminationAppointmentId);
		else if(name.equals("theoreticalExaminationNormalscore")) {
			mapper.updatenormalscore(score,theoreticalExaminationAppointmentId);
		}
		else
			mapper.updatemidscore(score,theoreticalExaminationAppointmentId);
		TheoreticalExaminationAppointment appointment=mapper.selectByPrimaryKey(theoreticalExaminationAppointmentId);
		Double Totalscore;
		if(appointment.getTheoreticalExaminationAppointmentScore()==null || appointment.getTheoreticalExaminationNormalscore()==null || appointment.getTheoreticalExaminationMidscore()==null)
			Totalscore= null;
		else {
			Double percentageNormal = Double.parseDouble(systemConfigservice.getvalue("theo_examination_normal")) * 1.0 / 100;
			Double percentageMid = Double.parseDouble(systemConfigservice.getvalue("theo_examination_mid")) * 1.0 / 100;
			Double percentageScore = Double.parseDouble(systemConfigservice.getvalue("theo_examination_end")) * 1.0 / 100;
			Totalscore = appointment.getTheoreticalExaminationNormalscore() * percentageNormal + appointment.getTheoreticalExaminationMidscore() * percentageMid + appointment.getTheoreticalExaminationAppointmentScore() * percentageScore;
		}
		mapper.updatetotalscore(Totalscore,theoreticalExaminationAppointmentId);
		return Totalscore;
	}

	public List<TheoreticalExaminationAppointment> getData(Integer systemYearId,Double theoreticalExaminationAppointmentScore,Integer theoreticalExaminationId,String userStudentInfoNum){
	    return mapper.getData(systemYearId,theoreticalExaminationAppointmentScore,theoreticalExaminationId,userStudentInfoNum);
	}

}
